function handles = analysisHandlerForTraceAvg(handles)
% analysisHandlerForTraceAvg
%
% Manages analysis of variable numbers of maps, using trace averaging
% approach for parsing pixels into direct vs non-direct
%
% overview of algorithm:
% - regular analysis
% - average the traces
% - analyze the averaged traces: parse pixels into direct vs non-direct
% - override the prior parsing with the new parsing
%
% Editing:
% gs july 2005 -- modified for trace avg approach
% ----------------------------------------------------

% % handles.data.analysis.cellAttachedCheck = 0;
% % % Note: this is basically a flag, indicating that we're not in EP analysis mode; probably can eliminate this
% % 
% % % reset the analysis stack and other stuff
% % handles = mapStacker(handles, 'reset');
% % handles = resetAnalysis('reanalyze', handles);
% % 
% % % loop through the individual map trials
% % for m = 1 : handles.data.analysis.numberOfMaps
% %     
% %     string = (['handles.data.map.map' num2str(m)]);
% %     eval(['handles.data.map.mapActive = ' string ';']);
% %     
% %     handles = analyzeInputMapArray(handles);
% %     
% %     % add the map data to the analysis stack
% %     mode = 'add';
% %     handles = mapStacker(handles, mode);    
% %     
% %     handles.data.map.mapAvg.synThreshpAmV = [handles.data.map.mapAvg.synThreshpAmV    handles.data.map.mapActive.synThreshpAmV];
% %     handles.data.map.mapAvg.baselineSD = [handles.data.map.mapAvg.baselineSD    handles.data.map.mapActive.baselineSD];
% %     handles.data.map.mapAvg.spontEventRate = [handles.data.map.mapAvg.spontEventRate    handles.data.map.mapActive.spontEventRate];
% %     
% %     eval([string ' = handles.data.map.mapActive;']);
% % end
% % 
% % % avg and update certain analysis results in the gui
% % handles.data.map.mapAvg.synThreshpAmV = mean(handles.data.map.mapAvg.synThreshpAmV);
% % handles.data.map.mapAvg.baselineSD = mean(handles.data.map.mapAvg.baselineSD);
% % handles.data.map.mapAvg.spontEventRate = mean(handles.data.map.mapAvg.spontEventRate);
% % 
% % set(handles.synThreshpAmV, 'String', num2str(round(100*handles.data.map.mapAvg.synThreshpAmV)/100));
% % set(handles.baselineSD, 'String', num2str(round(100*handles.data.map.mapAvg.baselineSD)/100));
% % set(handles.spontEventRate, 'String', num2str(round(100*handles.data.map.mapAvg.spontEventRate)/100));
% % 
% % % avg and plot the maps
% % mode = 'mean';
% % handles = mapStacker(handles, mode);
% % if ~isempty(handles.data.map.mapStack.mapAvgMin)
% %     %     if get(handles.chkShowHistoAnalysis, 'Value');
% %     handles = ampHistoAnalysis(handles);
% %     %     end
% %     arrayAvgPlots(handles);
% % else
% %     beep
% %     disp('processSetFamily: No average maps available for plotting.')
% % end
% % 
% % s = -sum(handles.data.map.mapStack.mapStackHistoThresh, 3);
% % s(isnan(s))=inf;
% % disp('map.eventNumSum = [');
% % disp(s);
% % disp('];');
% % handles.data.map.mapStack.mapAvgEventNumSum = abs(s);
% % % TODO: consolidate improve the dump-to-command-window 
% % 
% % % avg cell params
% % if get(handles.chkWithRsRm, 'Value')
% %     handles = avgCellParameters(handles);
% % end

%% NEW PART -- trace averaging -- starts here

% average the traces, and display
handles = traceAveraging(handles);
handles = arrayTracesAveraged(handles);

% make a new map pattern (# in pattern == array index) 
%     i.e., 1 17 ... etc ... 
%           2 18 ... etc ...
%           3 19 ... etc ...]
% [r,c] = size(pattern);
r = handles.data.map.traceAvg.patternSize(1);
c = handles.data.map.traceAvg.patternSize(2);
newPattern = reshape([1 : r * c], r, c);
handles.data.map.traceAvg.mapPattern = newPattern;

% set up additional necessary traceAvg fields, borrowing from the most recently active map
handles.data.map.traceAvg.mapSpacing = handles.data.map.mapActive.mapSpacing;
handles.data.map.traceAvg.directory = handles.data.map.mapActive.directory;

% now make the tracAvg data set the active one ...
handles.data.map.mapActive = handles.data.map.traceAvg;

% ... and run the averaged trace data through the event finder to detect direct events
handles = analyzeArrayOfAveragedTraces(handles);




% TODO: need to apply the new direct/non-direct parsing

% gs 5 sept 2005: The following is a short-term fix, for Leopoldo

% merge the 'old' direct and non-direct maps into one
A = handles.data.map.mapStack.mapAvgMean;
B = handles.data.map.mapStack.mapAvgMeanED;
C = nansum([reshape(A,1,[]); reshape(B,1,[])]);
fullMap = reshape(C, size(A,1), size(A,2));

% direct/non-direct map, logical (binary) version
E = handles.data.map.mapActive.mapDataArray(:,:,3);
directLogical = E ~= 0; % binary representation of direct response map
nonDirectLogical = E == 0; % binary representation of direct response map

% actual 'new' direct map
directMap = fullMap.*directLogical;
directMap(directMap == 0) = NaN;

% actual 'new' non-direct map
nonDirectMap = fullMap.*nonDirectLogical;
nonDirectMap(nonDirectMap == 0) = NaN;

% store stuff in handles variable
handles.data.map.traceAvg.fullMap = fullMap;
handles.data.map.traceAvg.directLogical = directLogical;
handles.data.map.traceAvg.nonDirectLogical = nonDirectLogical;
handles.data.map.traceAvg.directMap = directMap;
handles.data.map.traceAvg.nonDirectMap = nonDirectMap;

% add a few things so the trace browser will work
handles.data.map.traceAvg.baseName = 'averaged traces.';

